package adger.me.yili.view.impl;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import adger.me.yili.R;
import adger.me.yili.adapter.OrderAdapter;
import adger.me.yili.utils.ACache;
import adger.me.yili.view.IAllOrderView;

@SuppressLint("ValidFragment")
public class EndOrderFragment extends Fragment implements IAllOrderView {
    private String mTitle;
    private RecyclerView recyclerView;
    private OrderAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;
    private JSONArray mLists;
//    private OrderPresenter mOrderPresenter;
    private ACache mACache;

    public static EndOrderFragment getInstance(String title) {
        EndOrderFragment sf = new EndOrderFragment();
        sf.mTitle = title;
        return sf;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_end_order, null);
        recyclerView = (RecyclerView) view.findViewById(R.id.order_list);
//        mOrderPresenter = new OrderPresenter(getContext());
        mACache = ACache.get(getContext());

        initData();

        return view;
    }

    private void initData() {
        try {
            JSONObject data = mACache.getAsJSONObject("all_orders");
            if (data != null) {
                JSONArray list = data.getJSONArray("orders");
                JSONArray new_list = new JSONArray();
                if (list != null) {
                    for (int i = 0; i < list.length(); i++) {
                        if (list.getJSONObject(i).getInt("claimed") == 2 || list.getJSONObject(i).getInt("claimed") == 1) {
                            new_list.put(list.getJSONObject(i));
                        }
                    }
                    mLists = new_list;
                    System.out.println(new_list);
                }
            }else {
                Toast.makeText(getContext(),"没有数据,请检查你的网络",Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (mLists != null) {
            loadDatas();
        }
    }

    private void loadDatas() {
        mAdapter = new OrderAdapter(getContext(), mLists);
        // 设置RecycleView的布局管理
        mLinearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(mLinearLayoutManager);

        //设置RecycleView的Item间的分割线
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter);
    }

    @Override
    public void getData() {

    }

    @Override
    public void updataView() {

    }
}